package com.hpin.mlxteacher.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.ui.LoginActivity;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.Constant;
import com.hpin.mlxteacher.utils.HttpUtil;
//import com.umeng.analytics.MobclickAgent;

/**
 * 修改密码页面
 * Created by Aaron on 15/7/31.
 */
public class ChangePswActivity extends BaseActivity {

    private EditText et_old_psw;
    private EditText et_new_psw;
    private EditText et_new_psw_repeat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chage_psw);
        initWidget();
    }

    private void initWidget() {

        ImageView tv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_title.setText(getResources().getString(R.string.change_pwd));
        tv_right.setText(getResources().getString(R.string.done));

        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        et_old_psw = (EditText) findViewById(R.id.et_old_psw);
        et_new_psw = (EditText) findViewById(R.id.et_new_psw);
        et_new_psw_repeat = (EditText) findViewById(R.id.et_new_psw_repeat);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right:

                String oldPsw = et_old_psw.getText().toString().trim();
                String newPsw = et_new_psw.getText().toString().trim();
                String newPswRepeat = et_new_psw_repeat.getText().toString().trim();

                if (CommonUtils.isNull(oldPsw)) {

                    showToast("请输入旧密码");
                    return;
                }
                if (CommonUtils.isNull(newPsw)) {

                    showToast("请输入新密码");
                    return;
                }

                if (newPsw.length() < 6 || newPsw.length() > 16) {
                    showToast("密码在6-16位之间");
                    return;
                }

                if (!newPsw.equals(newPswRepeat)) {
                    showToast("两次输入的新密码不一致");
                    return;
                }

                JSONObject j = new JSONObject();
                j.put("token", sp.getString(Constant.token, ""));
                j.put("newpwd", CommonUtils.toMd5(et_new_psw.getText().toString().trim().getBytes()).toUpperCase());
                j.put("oldpwd", CommonUtils.toMd5(et_old_psw.getText().toString().trim().getBytes()).toUpperCase());

                HttpUtil.setPostRequest("修改中...", this, Constant.WEB_ROOT + Constant.UPDATEPWD,
                        j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.SUCCESS) {
                                showToast(Constant.DATA);

                                Intent intent = new Intent(ChangePswActivity.this, LoginActivity.class);
                                startActivity(intent);

                                finish();

                            } else {
                                showToast(Constant.showErrorMsg(result.getInt("code"), ChangePswActivity.this));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }
                    }
                }, null);
                break;

        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());

    }
}
